/*
Learning from Law Enforcement
Libor Dusek and Christian Traxler

Do-file producing regression results and figures in the paper and (online) appendix

*************************************
*** DESCRIPTION AND PRELIMINARIES
**************************************

*** This do file: RD_02.do
    Runs the key RD analyses at the high-fine cutoff (cutoff 2)
    TOC:
    I.  DEFINE SAMPLE & VARIABLES
    II. RD ANALYSES: Car Level
    III.RD ANALYSES: Ride Level
    IV. RD ANALYSES: Good vs Bad Traffic Conditions
    V.  RD ANALYSES: Heterogeneity
    VI. RD ANALYSES: Placebo Cutoffs
    VII.RD ANALYSES: Bandwidth Sensitivity Analysis

*** Required packages:
    rdrobust, parmest

*** The input dataset:
    Dusek_Traxler_RD_a4.dta
	
*** Output is stored in output directories: OUTPUT/RD_TAB  OUTPUT/RD_FIG , and OUTPUT/RD_temp
	Names of outpupt files correspond to the Table/Figure numbers in the paper and (online) appendix
	
*** List of Figures and Tables produced:
    See Section C of the ReadMe File.
*/

cap clear
set more off

/* Set directories */
global dir C:/yourdir/
cd    $dir
global datadir $dir/datasets/
global outputdir outfiles/
capture mkdir ${outputdir}
capture mkdir ${outputdir}/RD_TAB
capture mkdir ${outputdir}/RD_FIG
capture mkdir ${outputdir}/RD_temp

cap log close
log using    $dir/${outputdir}/log_RD_02.txt, replace

/* Set Global Paraemters */
global A = 4
*Length of Assignment Period, in months (A)
global F = 4
*Length of Follow-Up Period, in months (F)

/* Load Data with an Assignment Period of A months */
use $datadir/Dusek_Traxler_RD_a${A}.dta



/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* I.    DEFINE SAMPLE & VARIABLES
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.A   DEFINE SAMPLE
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
rename period_follup_${F} period_follup
drop period_follup_*
rename period_follup period_post
rename dum_haspost_${F} dum_haspost
drop dum_haspost_*
rename period_pre_${F} period_pre
drop period_pre_*

/* We only keep "relevant" observations during the pre or post [=outcome]
   period */
keep if (period_pre==1 | period_post==1 | dum_trigger==1)

/* Focus on observations with a trigger date before the enforcement rules were
   changed (early July 2017) */
keep if day_trigger<td(1jul2017)

/* Defines assignment speed (S_i in Section 4.1 of the paper, "speed_Z" here),
   normalized by the high-fine cutoff (23km/h above the speed limit  */
gen speed_Z = maxspeed - 23

/* focus on sample around Cutoff 2: maxspeed above Cutoff 1 (14km/h above
   limit) and below the 3rd cutoff (43km/h above the limit) */
keep if speed_Z>-9 & speed_Z<20

/* The reported estimates (at the 2nd cutoff) drop this (single) car - for
   reasons we can no longer identify; including it does not alter results. */
drop if id == 499389

/* define first relevant "episode" (after sample restrictions) */
/* cut sample, focus on first relevant episode */
bysort id: gegen epi_min=min(episode)
keep if episode==epi_min
drop epi_min

/* Identifier for first relevant drive through (pre period; hence: id defined
   independently of whether a ride is observed in the outcome period */
by id: gegen xxid_record_pre_min  = min(id_record) if (period_pre==1 | dum_trigger==1)
by id: gegen id_record_pre_min  = mean(xxid_record_pre_min)
drop xxid_record_*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.B   PRIMARY OUTCOME AND TREATMENT VARIABLES
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/*TREATMENT RATE (1): share of rides that happened after being ticketed */
by id:  gegen xxticketed_m = mean(dum_ticketed) if period_post==1
by id: gegen ticketed_m = mean(xxticketed_m)
drop xxticketed*

/*High Fine Ticket Indicator */
gen     xx_highfine=0
replace xx_highfine=1 if dum_trigger==1 & violcat2==1
by id: gegen high_fine = max(xx_highfine)
gen dum_treated_high     = dum_ticketed     * high_fine

/*TREATMENT RATE (2): share of rides after receiving HIGH FINE ticket */
by id:  gegen xxticketed_m = mean(dum_treated_high) if period_post==1
by id: gegen treated_high_m = mean(xxticketed_m)
drop xx_* xxticketed*

/*AVERAGE SPEED  (outcome var) */
by id:  gegen xxspeedm_m    = mean(speedm) if period_post==1
by id:  gegen speedm_m    = mean(xxspeedm_m)
drop xxspeedm_*

/*SPEEDING RATE (outcome var), share of rides above speed limit */
by id:  gegen xxspeeding_m     = mean(speeding) if period_post==1
by id:  gegen speeding_m     = mean(xxspeeding_m)
drop xxspeeding_*

/*VIOLATION RATE (outcome var), i.e., rides w/ speed above enforcement cutoff*/
by id:  gegen xxviol_m     = mean(violation) if period_post==1
by id:  gegen violation_m     = mean(xxviol_m)
drop xxviol_*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.C   COMPLEMENTARY OUTCOME AND TREATMENT VARIABLES
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/*SPEED PERCENTILES */
by id:  gegen xxspeedm_p50  = median(speedm) if period_post==1
by id:  gegen speedm_p50  = mean(xxspeedm_p50)
by id:  gegen xxspeedm_p75  = pctile(speedm) if period_post==1, p(75)
by id:  gegen speedm_p75  = mean(xxspeedm_p75)
by id:  gegen xxspeedm_p90  = pctile(speedm) if period_post==1, p(90)
by id:  gegen speedm_p90  = mean(xxspeedm_p90)
drop xxspeedm_*

/*AVERAGE SPEED - at same and other (= non-trigger) camera zone*/
by id:  gegen xxspeedm_m_same    = mean(speedm) if period_post==1 & rad_trigger==radarid
by id:  gegen speedm_m_same    = mean(xxspeedm_m_same)
by id:  gegen xxspeedm_m_othr    = mean(speedm) if period_post==1 & rad_trigger~=radarid
by id:  gegen speedm_m_othr    = mean(xxspeedm_m_othr)

/*SPEEDING RATE - at same and other (= non-trigger) camera zone*/
by id:  gegen xxspeeding_m_same     = mean(speeding) if period_post==1 & rad_trigger==radarid
by id:  gegen speeding_m_same     = mean(xxspeeding_m_same)
by id:  gegen xxspeeding_m_othr     = mean(speeding) if period_post==1 & rad_trigger~=radarid
by id:  gegen speeding_m_othr     = mean(xxspeeding_m_othr)
drop xxspeeding_*

/* Number of Rides in outcome period */
gen temp=1
by id: gegen xxnrides_post = sum(temp) if period_post==1
by id: gegen nrides_post = mean(xxnrides_post)
replace nrides_post=0 if nrides_post==.
cap drop temp

/* Dummy indicating at least 1 ride in outcome period */
gen     ever_return=1
replace ever_return=0 if nrides_post==0 & nrides_post~=.

/* Average timediff during outcome (!) period */
by id: gegen xxpost_timediff = mean(timediff) if period_post==1
by id: gegen post_timediff = mean(xxpost_timediff)
drop xxpost*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  I.D  VARS for BALANCING CHECKS
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Number of Rides in PRE(!) treatment period */
gen temp=1
by id: gegen xxnrides_pre = sum(temp) if (period_pre==1 | dum_trigger==1)
by id: gegen   nrides_pre = mean(xxnrides_pre)
drop xxnrides_* temp

/* Driving frequency in pre-treatment (!) period */
gen length_pre = 1 + day_endwait - day_first
gen driving_frequency = nrides_pre/length_pre

/* Log of time diff to next car traveling ahead (measured at entry point of
   a given speed camera) */
gen log_interval=log(timediff)

*time of day I
gen     h_morn=0
replace h_morn=1 if hofday>=5 & hofday<12
gen     h_aftern=0
replace h_aftern=1 if hofday>=12 & hofday<20
gen     h_night=0
replace h_night=1 if hofday>=20 | hofday<5

*time of day II
gen     h_0=0
replace h_0=1 if hofday>=0 & hofday<6
gen     h_6=0
replace h_6=1 if hofday>=6 & hofday<9
gen     h_9=0
replace h_9=1 if hofday>=9 & hofday<12
gen     h_12=0
replace h_12=1 if hofday>=12 & hofday<15
gen     h_15=0
replace h_15=1 if hofday>=15 & hofday<18
gen     h_18=0
replace h_18=1 if hofday>=18 & hofday<21
gen     h_21=0
replace h_21=1 if hofday>=21 & hofday<24

qui tab dofweek, gen(dow_)
qui tab mofyear, gen(month_)

/* Defines Variables for "Trigger Observation" */
foreach Y of varlist log_interval h_6 h_9 h_12 h_15 h_18 h_21 dow_2 dow_3 dow_4 dow_5 dow_6 dow_7 h_morn h_aftern h_night  ///
      weekend temp_2m wind  month_2 month_3 month_4 month_5 month_6 month_7 month_8 month_9 month_10 month_11 month_12 {
 qui by id:  gegen xx_m_`Y'_trigger    = mean(`Y') if dum_trigger==1
 qui by id:  gegen `Y'_T               = mean(xx_m_`Y'_trigger)
 drop xx_m_*
 }
qui compress


/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* II. RD ANALYSES, HIGH-FINE CUTOFF (cut 2) - Car Level Analysis (Main Parts)
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* subsequently defines sample for a given estimation/computation */
gen ESTSAMPLE=0

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.A     Table A.5 : Reduced Form Estimates: de-tour responses?
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Focus: cross-section of cars (with assignment speed around cutoff 2),
   even if no observation recorded during outcome period; Q: Lower driving
   frequency post-treatment ?  */

replace ESTSAMPLE=1 if id_record==id_record_pre_min

rdrobust nrides_post speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su nrides_post if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A5_cut2.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(nrides_post) replace

foreach Y of varlist ever_return  {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 outreg2 using ${outputdir}/RD_TAB/Tab_A5_cut2.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(`Y') append
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Main Analysis Sample: cross-section of cars (=> car level estimates),
  requiring at least one ride in outcome period
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

replace ESTSAMPLE=0
replace ESTSAMPLE=1 if id_record==id_record_pre_min & dum_haspost==1

/* Computed Bins and Binned Means for RD Graphs */
qui gen bin_temp=.
local b = 0.5
forvalues i = -9(`b')10 {
 qui replace bin_temp=`i' if speed_Z>=(`i') & speed_Z<(`i'+`b')
}
gegen bin1=group(bin_temp)
gen  bin2 = bin_temp + `b'/2
drop bin_temp

foreach Y of varlist ticketed_m treated_high_m speeding_m speedm_m  {
 cap drop mean_`Y'
  gegen mean_`Y' = mean(`Y') if ESTSAMPLE==1, by(bin2)
 }

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.B     Table A.4, Panel B : Reduced Form Estimates and
*           Figures A6, 2b, 3b, 3d
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Treated ("TICKETED") */
rdrobust ticketed_m speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su ticketed_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(ticketed) replace

/* Figure A6  */
twoway  (lpolyci ticketed_m  speed_Z if speed_Z<0 & speed_Z>-9 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (lpolyci ticketed_m  speed_Z if speed_Z>0 & speed_Z<10 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (scatter mean_ticketed_m bin2 if inrange(speed_Z,-10,10), mcolor(navy) msize(medsmall)), xline(0, lcolor(gs0) lpattern(dash)) legend(off) ///
        xlabel(-10(2.5)10) xtitle("Assignment Speed, S") ytitle("Treated Rides") graphregion(color(white)) plotregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_A6_ticketed_cut2.pdf, replace as(pdf)


/* High-fine Treated  */
rdrobust treated_high_m speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su ticketed_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(high-fine) append

/* Figure 2b */
twoway  (lpolyci treated_high_m  speed_Z if speed_Z<0 & speed_Z>-9 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (lpolyci treated_high_m  speed_Z if speed_Z>0 & speed_Z<10 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (scatter mean_treated_high_m bin2 if inrange(speed_Z,-10,10), mcolor(navy) msize(medsmall)), xline(0, lcolor(gs0) lpattern(dash)) legend(off) ///
        xlabel(-10(2.5)10) xtitle("Assignment Speed, S") ytitle("Treated Rides") graphregion(color(white)) plotregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_2b_ticketed.pdf, replace as(pdf)


/* Speeding */
rdrobust speeding_m speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) append

/* Figure 3b : Speeding Rate (Cutoff 2) */
twoway  (lpolyci speeding_m  speed_Z if speed_Z<0 & speed_Z>-9 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (lpolyci speeding_m  speed_Z if speed_Z>0 & speed_Z<10 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (scatter mean_speeding_m bin2 if inrange(speed_Z,-10,10), mcolor(navy) msize(medsmall)), xline(0, lcolor(gs0) lpattern(dash)) legend(off) ///
        xtitle("Assignment Speed, S") ytitle("Speeding Rate") graphregion(color(white)) plotregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_3b.pdf, replace as(pdf)

/* (Re-)Offending */
rdrobust violation_m speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su violation_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(offense) append

/* Mean Speed */
rdrobust speedm_m speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed) append

/* Figure 3d : Speed (Cutoff 2) */
twoway  (lpolyci speedm_m  speed_Z if speed_Z<0 & speed_Z>-9 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (lpolyci speedm_m  speed_Z if speed_Z>0 & speed_Z<10 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (scatter mean_speedm_m bin2 if inrange(speed_Z,-10,10), mcolor(navy) msize(medsmall)), xline(0, lcolor(gs0) lpattern(dash)) legend(off) ///
        xtitle("Assignment Speed, S") ytitle("Mean Speed") graphregion(color(white)) plotregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_3d.pdf, replace as(pdf)

/* "Top" Speed (p90) */
rdrobust speedm_p90 speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su speedm_p90 if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed p90) append


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.C     Data for bandwidth sensitivity check
            => see Section VII. (below) in this do file
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* RANGE for Bandwidth Sensitivity Checks;  Note 1: constant factor (1.4) used
   for bias correction. Note 2: all based on reduced form estimates. */

forvalues H = 0.5(0.25)8 {
 local B = 1.4*`H'
 local H2=100*`H'
 qui rdrobust speeding_m  speed_Z if ESTSAMPLE==1, all p(1) h(`H') b(`B') vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/RedF_Cut2_speeding_BW_`H2'.dta,replace)

 qui rdrobust speedm_m  speed_Z if ESTSAMPLE==1, all p(1) h(`H') b(`B') vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/RedF_Cut2_speed_BW_`H2'.dta, replace)
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.D     Balance/Smoothness of Pre-Treatment Vars: Table A3
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Tables A3, Column 1 */
rdrobust driving_frequency  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 qui su driving_frequency if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A3_Row-1.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(driving_frequ) replace

/* Tables A3, Columns 2--10 */
foreach Y of varlist nrides_pre plate_A plate_S log_interval_T temp_2m_T wind_T  h_6 h_9 h_12 {
 qui rdrobust `Y'  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  qui outreg2 using ${outputdir}/RD_TAB/Tab_A3_Row-1.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(`Y') append
}

/* Tables A3, Column 11 */
rdrobust h_15  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 qui su h_15 if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A3_Row-2.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(h_15) replace

/* Tables A3, Columns 12--20 */
foreach Y of varlist h_18 h_21  dow_2_T dow_3_T dow_4_T dow_5_T dow_6_T dow_7_T month_2_T {
 qui rdrobust `Y'  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  qui outreg2 using ${outputdir}/RD_TAB/Tab_A3_Row-2.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(`Y') append
}

/* Tables A3, Column 21 */
rdrobust month_3_T  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 qui su month_3_T if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A3_Row-3.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(month_3_T) replace

/* Tables A3, Columns 22--30 */
foreach Y of varlist month_4_T month_5_T month_6_T month_7_T month_8_T month_9_T month_10_T month_11_T month_12_T {
 qui rdrobust `Y'  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  qui outreg2 using ${outputdir}/RD_TAB/Tab_A3_Row-3.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(`Y') append
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.E     Table 2, Panel C : Main Wald Estimates, Car Level
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Speeding */
rdrobust speeding_m  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) replace

/* (Re-) Offending */
rdrobust violation_m  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su violation_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(offense) append

/* Mean speed */
rdrobust speedm_m  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speedm) append

/* Table 2, Panel A, Columns 4 -- 6 */
foreach Y of varlist              speedm_p50 speedm_p75 speedm_p90 {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  local bw = h_mserd
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.F     Table 3, Panel C : Same vs Other Camera Zones, Car Level
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust speeding_m_same speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m)  all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m_same if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/same) replace

rdrobust speeding_m_othr speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m_othr if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/other) append

rdrobust speeding_m_same speed_Z if ESTSAMPLE==1 & speeding_m_othr~=., fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m_same if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & speeding_m_othr~=.
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/same) append

rdrobust speeding_m_othr speed_Z if ESTSAMPLE==1 & speeding_m_same~=., fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m_othr if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & speeding_m_same~=.
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/other) append

rdrobust speedm_m_same speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m_same if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/same) append

rdrobust speedm_m_othr speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m_othr if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/other) append

rdrobust speedm_m_same speed_Z if ESTSAMPLE==1 & speedm_m_othr~=., fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m_same if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & speeding_m_othr~=.
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/same) append

rdrobust speedm_m_othr speed_Z if ESTSAMPLE==1 & speedm_m_same~=., fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m_othr if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & speeding_m_same~=.
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/other) append

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* III. RD ANALYSES, HIGH-FINE CUTOFF (cut 2) - Ride Level Analysis
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* Different Sample => RIDE LEVEL Estimates: all rides in outcome period */
replace ESTSAMPLE=0
replace ESTSAMPLE=1 if dum_haspost==1 & period_post==1

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  III.A     Table 2, Panel D : Main Wald Estimates, Ride Level
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust speeding  speed_Z if ESTSAMPLE==1, fuzzy(dum_treated_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) replace

foreach Y of varlist violation speedm  {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1, fuzzy(dum_treated_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
  local bw = h_mserd

  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  III.B     Table 3, Panel D : Same vs Other Camera Zones, Ride Level
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Count of rides at Same/Other camera zones (in outcome period */
gen temp=1
bysort id: gegen xxnrides_post_same = sum(temp) if period_post==1 & rad_trigger==radarid
by id:     gegen nrides_same = mean(xxnrides_post_same)
replace nrides_same=0 if nrides_same==.
by id: gegen xxnrides_post_othr = sum(temp) if period_post==1 & radarid~=rad_trigger
by id:     gegen nrides_othr = mean(xxnrides_post_othr)
replace nrides_othr=0 if nrides_othr==.
drop xxnrides* temp
qui compress

gen     same_camera=0
replace same_camera=1 if rad_trigger==radarid

/* Speeding (Col 1 -- 4) */

rdrobust speeding speed_Z                     if ESTSAMPLE==1 & same_camera==1, fuzzy(dum_treated_high)  all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/same) replace

rdrobust speeding speed_Z                     if ESTSAMPLE==1 & same_camera==0, fuzzy(dum_treated_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/other) append

rdrobust speeding speed_Z                     if ESTSAMPLE==1 & same_camera==1 & nrides_othr>0, fuzzy(dum_treated_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==1 & nrides_othr>0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/same) append

rdrobust speeding speed_Z                     if ESTSAMPLE==1 & same_camera==0 & nrides_same>0, fuzzy(dum_treated_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==0 & nrides_same>0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/other) append

/* Speed (Col 5 -- 8) */

rdrobust speedm speed_Z                     if ESTSAMPLE==1 & same_camera==1, fuzzy(dum_treated_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speedm if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/same) append

rdrobust speedm speed_Z                     if ESTSAMPLE==1 & same_camera==0, fuzzy(dum_treated_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speedm if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/other) append

rdrobust speedm speed_Z                     if ESTSAMPLE==1 & same_camera==1 & nrides_othr>0, fuzzy(dum_treated_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speedm if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==1 & nrides_othr>0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/same) append

rdrobust speedm speed_Z                     if ESTSAMPLE==1 & same_camera==0 & nrides_same>0, fuzzy(dum_treated_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speedm if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==0 & nrides_same>0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/other) append

drop nrides_same nrides_othr


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  III.C     Fig 4b : Shift in speed distribution
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

gen relspeedm = speedm - speedlimit

twoway (kdensity relspeedm if  ESTSAMPLE==1 & speed_Z<0 & speed_Z>-1, clcolor(dkgreen) lwidth(medium) xline(0 14, lcolor(gs8) lpattern(longdash) lwidth(vthin) ) ///
    legend(off) range(-22.5 22.5)  xlabel(-20(10)20) xtitle("Speed (relative to limit)") ytitle("Density") graphregion(color(white)) plotregion(color(white)) ) ///
    (kdensity relspeedm if  ESTSAMPLE==1 & speed_Z>0 & speed_Z<1, range(-22.5 22.5)  clcolor(red) lpattern(dash) lwidth(medium))
 graph export ${outputdir}/RD_FIG/Fig_4b_distribution.pdf, replace as(pdf)


/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* IV. RD ANALYSES, HIGH-FINE CUTOFF (cut 2) - Good vs Bad driving Conditions
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* Return to initial sample: cross-section of cars w/ at least one ride in
   outcome period */

replace ESTSAMPLE=0
replace ESTSAMPLE=1 if id_record==id_record_pre_min & dum_haspost==1

/* Indicator for relevant Cross Section of Cars */
cap drop SAMPLE_CARS
gen     xxsample_cars=0
replace xxsample_cars=1 if ESTSAMPLE==1
gegen SAMPLE_CARS=max(xxsample_cars), by(id)
drop xxsample_*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.A   DEFINES COMPLEMENATARY VARIABLES - Main Analysis
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* defines good driving conditions */
su timediff if SAMPLE_CARS==1 & period_post==1, d

gen      good_conditions = 0 if timediff<=r(p50) & timediff~=. & SAMPLE_CARS==1  & period_post==1
replace  good_conditions = 1 if  timediff>r(p50) & timediff~=. & SAMPLE_CARS==1  & period_post==1

/* Outcome: SPEEDING RATE in outcoe period, for good/bad conditions */
by id:  gegen xxspeeding_good     = mean(speeding) if period_post==1 & good_conditions==1
by id:  gegen speeding_good     = mean(xxspeeding_good)
by id:  gegen xxspeeding_bad     = mean(speeding) if period_post==1 & good_conditions==0
by id:  gegen speeding_bad     = mean(xxspeeding_bad)
drop    xxspeeding_*

/* Outcome: AVERAGE SPEED in outcome period, for good/bad conditions */
by id:  gegen xxspeedm_good    = mean(speedm) if period_post==1  & good_conditions==1
by id:  gegen speedm_good    = mean(xxspeedm_good)
by id:  gegen xxspeedm_bad    = mean(speedm) if period_post==1  & good_conditions==0
by id:  gegen speedm_bad    = mean(xxspeedm_bad)
drop xxspeedm_*

/* Outcome: "Top SPEED" (p90) in outcome period, for good/bad conditions */
by id:  gegen xxspeedm_p90_good    = pctile(speedm) if period_post==1  & good_conditions==1, p(90)
by id:  gegen speedm_p90_good    = mean(xxspeedm_p90_good)
by id:  gegen xxspeedm_p90_bad    = pctile(speedm) if period_post==1  & good_conditions==0, p(90)
by id:  gegen speedm_p90_bad    = mean(xxspeedm_p90_bad)
drop xxspeedm_*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.B   Table A.7, Panel C: Wald estiamtes for good/bad traffic conditions
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust speeding_good  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding-good) replace

rdrobust speedm_good  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-good) append

rdrobust speedm_p90_good  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_p90_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-good) append

rdrobust speeding_bad  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding-bad) append

rdrobust speedm_bad   speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-bad) append

rdrobust speedm_p90_bad   speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_p90_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-bad) append

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.C   Table A.7, Panel D: Wald estiamtes for good/bad traffic conditions,
*        for a constant set of cars (observed under good and bad conditions)
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

replace speeding_good   = . if ESTSAMPLE==1 & speeding_bad==.
replace speedm_good   = . if ESTSAMPLE==1 & speedm_bad==.
replace speedm_p90_good   = . if ESTSAMPLE==1 & speedm_p90_bad==.

replace speeding_bad   = . if ESTSAMPLE==1 & speeding_good==.
replace speedm_bad   = . if ESTSAMPLE==1 & speedm_good==.
replace speedm_p90_bad   = . if ESTSAMPLE==1 & speedm_p90_good==.

rdrobust speeding_good  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding-good) replace

rdrobust speedm_good  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-good) append

rdrobust speedm_p90_good  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_p90_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-good) append

rdrobust speeding_bad  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding-bad) append

rdrobust speedm_bad   speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-bad) append

rdrobust speedm_p90_bad   speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_p90_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-bad) append


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.D   DEFINES COMPLEMENATARY VARIABLES - Selection Analysis
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

preserve
keep if SAMPLE_CARS==1

/*GOOD-CONDITION RIDES, Share */
by id:  gegen xxgood_rides   = mean(good_conditions) if period_post==1
by id:  gegen good_rides     = mean(xxgood_rides)
drop xxgood_rides

/*GOOD- /BAD-CONDITION RIDES, Counts */
gegen xxgood_rides_N   = sum(good_conditions) if period_post==1, by(id)
gegen good_rides_N     = mean(xxgood_rides_N), by(id)
drop xxgood_rides_N

gen bad_conditions = 1 - good_conditions
gegen xxbad_rides_N   = sum(bad_conditions) if period_post==1, by(id)
gegen bad_rides_N     = mean(xxbad_rides_N), by(id)
drop xxbad_rides_N bad_conditions

/* Average Traffic Density => log(time-diff) */
by id:  gegen xxtraffic_dens     = mean(timediff) if period_post==1
by id:  gegen traffic_dens     = mean(xxtraffic_dens)
drop xxtraffic_*
gen log_timdiff_m =log(traffic_dens)

/* Average Hour-of-day (numeric), outcome(!) period */
by id:  gegen xxhour_post     = mean(hofday) if period_post==1
by id:  gegen hour_post     = mean(xxhour_post)
drop xxhour_*

/* Share of Morning / Night rides, outcome(!) period */
cap drop h_6
gen     h_6=0
replace h_6=1 if hofday>=6 & hofday<9
by id:  gegen xxhour_morn     = mean(h_6) if period_post==1
by id:  gegen hour_morn     = mean(xxhour_morn)
drop h_6

cap drop h_night
gen     h_night=0
replace h_night=1 if hofday>=20 | hofday<5
by id:  gegen xxhour_night     = mean(h_night) if period_post==1
by id:  gegen hour_night     = mean(xxhour_night)
drop xxhour_* h_night

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.E      Table C.1, Panel C : Selection into driving conditions
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust log_timdiff_m  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su log_timdiff_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C1_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(log_timdiff_m) replace

foreach Y of varlist good_rides_N bad_rides_N good_rides  hour_post hour_morn hour_night {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  local bw = h_mserd
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C1_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.E      Table C.1, Panel D : Selection; constrained sample
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust log_timdiff_m  speed_Z if ESTSAMPLE==1 & bad_rides_N>0 & good_rides_N>0, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su log_timdiff_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & bad_rides_N>0 & good_rides_N>0
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C1_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(log_timdiff_m) replace

foreach Y of varlist good_rides_N bad_rides_N good_rides  hour_post hour_morn hour_night {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1 & bad_rides_N>0 & good_rides_N>0, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  local bw = h_mserd
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & bad_rides_N>0 & good_rides_N>0
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C1_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

restore

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* V.   RD ANALYSES, HIGH-FINE CUTOFF (cut 2) - Heterogeneity (Tab C.4)
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* Sample: cross-section of cars w/ at least one outcome period ride */

replace ESTSAMPLE=0
replace ESTSAMPLE=1 if id_record==id_record_pre_min & dum_haspost==1

qui su driving_frequ      if ESTSAMPLE==1,d
qui gen      frequent_driver=0 if driving_frequ<=r(p50) & ESTSAMPLE==1
qui replace  frequent_driver=1 if driving_frequ> r(p50) & ESTSAMPLE==1 & driving_frequ~=.

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* V.A   Table C.4, Panel C : Split Sample, Outcome: Speeding
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* frequent vs infrequent drivers */
rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & frequent_driver==0, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & frequent_driver==0
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding, infrequent) replace

rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & frequent_driver==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & frequent_driver==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding, frequent) append

/* Number plate regions */
rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & plate_S==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_S==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding, "Local") append

rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & plate_A==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_A==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding, Prague) append

rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & plate_O==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_O==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding, Other) append

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* V.B   Table C.4, Panel D : Split Sample, Outcome: Mean Speed
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* frequent vs infrequent drivers */
rdrobust speedm_m  speed_Z if ESTSAMPLE==1 & frequent_driver==0, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & frequent_driver==0
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed, infrequent) replace

rdrobust speedm_m  speed_Z if ESTSAMPLE==1 & frequent_driver==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & frequent_driver==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed, frequent) append

/* Number plate regions */
rdrobust speedm_m  speed_Z if ESTSAMPLE==1 & plate_S==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_S==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed, "Local") append

rdrobust speedm_m  speed_Z if ESTSAMPLE==1 & plate_A==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_A==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed, Prague) append

rdrobust speedm_m  speed_Z if ESTSAMPLE==1 & plate_O==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_O==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed, Other) append


/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* VI.   RD ANALYSIS: Placebo Cutoffs around cutoff 2
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* The orginally executed do file skipped the line that fixed the seed. */
set seed 918273

keep if ESTSAMPLE==1

*/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* VI.A      Reduced Form Estimates at Placebo Cutoffs
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

gen speed_Z_random = .

/* First: shift cutoff 1kmh `to left', randomly add/substract random var1 [0,1]
   and run reduced form; store results. */
local t = 1
while `t' <= 500 {
 local random1 = runiform()
 local random2 = runiform()
 qui replace speed_Z_random = speed_Z - 1 + `random1' if `random2'<0.5
 qui replace speed_Z_random = speed_Z - 1 - `random1' if `random2'>0.5

 qui rdrobust speeding_m  speed_Z_random if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/placebo_cut2_speeding_`t', replace)
 qui rdrobust speedm_m  speed_Z_random if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/placebo_cut2_speed_`t', replace)
 local t = `t' + 1
}

/* Second: shift cutoff 1kmh `to right', and repeat same steps as above */
local t = 501
while `t' <= 1000 {
 local random1 = runiform()
 local random2 = runiform()
 qui replace speed_Z_random = speed_Z + 1 + `random1' if `random2'<0.5
 qui replace speed_Z_random = speed_Z + 1 - `random1' if `random2'>0.5

 qui rdrobust speeding_m  speed_Z_random if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/placebo_cut2_speeding_`t', replace)
 qui rdrobust speedm_m  speed_Z_random if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/placebo_cut2_speed_`t', replace)
 local t = `t' + 1
}

*/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* VI.B      Figure A.10, Subfig. b + d : Plot CDF of placebo estiamates
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Figure A.10, Subfig. (b) */
cap clear
use ${outputdir}/RD_temp/placebo_cut2_speeding_1.dta
forvalues t = 2/1000 {
 append using OUTPUT_JEEA/RD_temp/placebo_cut2_speeding_`t'.dta
}
keep if parm=="Robust"
keep estimate
cumul estimate, gen(cdf_base)

twoway scatter cdf_base estimate, sort msymbol(circle_hollow) msize(small) xline(0  -0.0199)  ylabel(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1", angle(horizontal)) ///
    xlabel(-0.1 "-0.10" -0.05 "-0.05" 0.00 "0.00" 0.05 "0.05" 0.10 "0.10", angle(horizontal)) ytitle("cumulative distribution function") xtitle("Reduced Form Estimates: Speeding") text(0.2 0.06 " ")
 graph export ${outputdir}/RD_FIG/Fig_A10_b_placebo_speeding.pdf, as(pdf) replace

count if estimate<-0.0199


/* Figure A.10, Subfig. (d) */
cap clear

use ${outputdir}/RD_temp/placebo_cut2_speed_1.dta
forvalues t = 2/1000 {
 append using OUTPUT_JEEA/RD_temp/placebo_cut2_speed_`t'.dta
}
keep if parm=="Robust"
keep estimate
cumul estimate, gen(cdf_base)

twoway scatter cdf_base estimate, sort msymbol(circle_hollow) msize(small) xline(0  -0.5938)  ylabel(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1", angle(horizontal)) ///
        xlabel(-1.5 "-1.5" -1 "-1.0"  -0.5 "-0.5"  0.00 "0.00" 0.5 "0.5" 1 "1.0" 1.5 "1.5" , angle(horizontal)) ytitle("cumulative distribution function") ///
        xtitle("Reduced Form Estimates: Speed") text(0.2 0.06 " ")
 graph export ${outputdir}/RD_FIG/Fig_A10_d_placebo_speed.pdf, as(pdf) replace

count if estimate<-0.5938


/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* VII.   RD ANALYSES, High-Fine Cutoff - Bandwidth Sensitivity (Fig A.8)
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* Work with data created in Section II.C (see above): speeding outcome  */
cap clear
use ${outputdir}/RD_temp/RedF_Cut2_speeding_BW_50.dta, clear
 gen bw = 0.5

forvalues H = 0.75(0.25)8 {
 local h = `H'*100
 append using ${outputdir}/RD_temp/RedF_Cut2_speeding_BW_`h'.dta
 replace bw = `H' if bw ==.
 }

/* keeping only the "Robust" estimator */
keep if parm=="Robust"
drop parm

label var min95 "Lower 95% CI"
label var max95 "Upper 95% CI"
label var bw "Bandwidth"
label var estimate "Estimate"

*/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* VII.A   Figure A.8, Subfigure (b)
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

twoway (line estimate bw, lw(medthick) lcolor(navy)) (line min95 bw, lpattern(longdash_dot) lcolor(maroon)) (line max95 bw,lpattern(longdash_dot) lcolor(maroon)), ///
       xtitle("Bandwidth") xlabel(0.5(0.5)8) ylabel(-0.15(0.05)0.15) ytitle("Estimates: Speeding") legend(off) graphregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_A8_b_Bandwidth_speeding.pdf,replace


/* Work with data created in Section II.C (see above): speed outcome */
cap clear
use ${outputdir}/RD_temp/RedF_Cut2_speed_BW_50.dta, clear
 gen bw = 0.5

forvalues H = 0.75(0.25)8 {
 local h = `H'*100
 append using ${outputdir}/RD_temp/RedF_Cut2_speed_BW_`h'.dta
 replace bw = `H' if bw ==.
 }

keep if parm=="Robust"
drop parm

label var min95 "Lower 95% CI"
label var max95 "Upper 95% CI"
label var bw "Bandwidth"
label var estimate "Estimate"

*/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* VII.B   Figure A.8, Subfigure (d)
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

twoway (line estimate bw, lw(medthick) lcolor(navy)) (line min95 bw, lpattern(longdash_dot) lcolor(maroon)) (line max95 bw,lpattern(longdash_dot) lcolor(maroon)), ///
        xtitle("Bandwidth") xlabel(0.5(0.5)8) ylabel(-3(0.5)3) ytitle("Estimates: Speed") legend(off) graphregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_A8_d_Bandwidth_speed.pdf,replace

clear

log close
